#include "talkwindow.h"
#include "ui_talkwindow.h"
#include <QDebug>

talkwindow::talkwindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::talkwindow)
{
    ui->setupUi(this);
}

talkwindow::~talkwindow()
{
    delete ui;
}

void talkwindow::append_show_recv_message(uint32_t ID, QString &message)
{
    for (auto &id_user : ID_user)
    {
        if (ID == id_user.first)
        {
            ui->textEditDialog->append(id_user.second + ": " + message);
            break;
        }
    }
}

void talkwindow::flush_client_info(QVector<QPair<uint32_t, QString>> &ID_user)
{
    this->ID_user = ID_user;
    ui->comboBoxClient->clear();
    for (auto &id_user : ID_user)
    {
        ui->comboBoxClient->addItem(id_user.second);
    }
}

void talkwindow::on_pushButtonClose_clicked()
{
    emit go_closed();
    this->close();
}

void talkwindow::on_pushButtonSend_clicked()
{
    // 获取编辑框内容
    QString sendStr = ui->textEditSend->toPlainText();
    ui->textEditDialog->append("Mine: "+sendStr);

    uint32_t ID = 0;

    QString user = ui->comboBoxClient->currentText();
    for (auto &id_user : ID_user)
    {
        if (0 == user.compare(id_user.second))
        {
            ID = id_user.first;
            break;
            qDebug()<<"ID:"<<ID;
        }
    }

    emit send_talk_msg(ID, sendStr);

//    QString str = ui->textEditWrite->toPlainText();
//    tcpSocket->write(str.toUtf8().data());
//    ui->textEditRead->append(array);
}
